package org.aeon.gamechatnest.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.aeon.gamechatnest.common.response.Result;
import org.aeon.gamechatnest.common.response.ResultBuild;
import org.aeon.gamechatnest.dto.AuthoritySearchDto;
import org.aeon.gamechatnest.dto.AuthorityVo;
import org.aeon.gamechatnest.exception.ApplicationException;
import org.aeon.gamechatnest.service.AuthorityService;
import org.aeon.gamechatnest.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: MissingAeon
 * @date: 2024/5/13 10:22
 * @description:
 */
@Tag(name = "AuthorityControllerApi", description = "权限控制器类API")
@RestController
@RequestMapping("/authority")
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;

    @Operation(summary = "获取所有权限列表", description = "获取所有权限列表")
    @GetMapping
    public Result getAuthorities(AuthoritySearchDto authoritySearchDto) {
        List<AuthorityVo> authorityVos = authorityService.find(authoritySearchDto);
        return ResultBuild.result().successful().data(authorityVos).build();
    }

    @PreAuthorize("hasAnyAuthority('authority:find','admin','creator') AND " +
            "@roleController.getUserServerId(#roleId) == T(org.aeon.gamechatnest.util.SecurityUtil).server")
    @Operation(summary = "获取角色权限", description = "获取角色权限", parameters = {
            @Parameter(name = "roleId", description = "角色id")
    })
    @GetMapping("/{roleId}")
    public Result getRoleAuthority(@PathVariable("roleId") Long roleId) {
        List<AuthorityVo> authority = authorityService.findAuthorityByRoleId(roleId);
        return ResultBuild.result().successful().data(authority).build();
    }


}
